package org.jeecg.modules.gzgl.controller;

import com.zhaodui.fxy.JSONHelper;
import com.zhaodui.fxy.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.gzgl.api.apientity;
import org.jeecg.modules.gzgl.entity.FxyGzgl00;
import org.jeecg.modules.gzgl.entity.FxyGzgl02;
import org.jeecg.modules.gzgl.entity.FxyGzgl03;
import org.jeecg.modules.gzgl.entity.FxyGzgl04;
import org.jeecg.modules.gzgl.service.IFxyGzgl00Service;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl02ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl03ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl04ServiceImpl;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Description: 公证公共接口
 * @Author: zhaodui
 * @Date: 2020-02-06
 * @Version: V1.0
 */
@RestController
@Api(tags="gzgl")
@RequestMapping("/gzgl/fxycom")
@Slf4j
public class FxyComController extends JeecgController<FxyGzgl00, IFxyGzgl00Service> {
    @Autowired
    private FxyGzgl04Controller fxyGzgl04Controller;

    @Autowired
    private FxyGzgl02ServiceImpl fxyGzgl02Service;

    @Autowired
    private FxyGzgl03ServiceImpl fxyGzgl03Service;

    @Autowired
    private FxyGzgl04ServiceImpl fxyGzgl04Service;
    @Value(value = "${baseorg.common.basemobile}")
    private String basemobile;
    /**
     * 添加
     *
     * @param comEntity
     * @return
     */
    @PostMapping(value = "/synchronous")
    public ResulFxy<?> synchronous(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) comEntity comEntity) {
        log.info("comEntity=" + comEntity.toString());
        ResulFxy resulFxy = new ResulFxy();
        resulFxy.setResultCode("success");
        resulFxy.setResultDesc("同步操作成功");
        return resulFxy;
    }


    /**
     * 添加
     *
     * @return
     */
    @GetMapping(value = "/getdata")
    @ApiOperation(value = "获取数据", notes = "获取数据")
    public ResulFxy<?> getdata(HttpServletRequest request, HttpServletResponse response ,
                               @RequestParam(name="sqltype", required = false) String sqltype ,
                               @RequestParam(name="outlenth", required = false) String outlenth ,
                               @RequestParam(name="para", required = false) String para,
                               @RequestParam(name="username", required = false) String username) {
        ResulFxy resulFxy = new ResulFxy();
        String url = "http://120.78.150.43/bootutil/fxjDate/list/cez";
        LinkedHashMap<String, Object> paramMap = new LinkedHashMap<String, Object>();
        paramMap.put("outlenth",outlenth);
        paramMap.put("sqltype",sqltype);
		paramMap.put("username",username);
        paramMap.put("para",para);
        String result= com.xiaoleilu.hutool.http.HttpUtil.post(url,paramMap);
        apientity apientity = JSONHelper.fromJsonToObject(result, org.jeecg.modules.gzgl.api.apientity.class);
        List<org.jeecg.modules.gzgl.api.apientity.ResultBean>   res =   apientity.getResult();



        if("1".equals(sqltype)){        //生成主借人
            for(org.jeecg.modules.gzgl.api.apientity.ResultBean t2:  res){
//                FxyGzgl02 t02 = new FxyGzgl02();
//                t02.setQuery07(t2.getOutX1());//订单编号
//                t02.setQuery12(t2.getOutX1());//个人用户的手机号
//                t02.setQuery13(t2.getOutX1());//用户姓名
//                t02.setQuery14(t2.getOutX1());//个人用户证件类型
//                t02.setQuery15(t2.getOutX1());//个人用户证件号码
//                t02.setQuery16(t2.getOutX1());//公证处标识
//                t02.setQuery17(t2.getOutX1());//邮箱
//                t02.setQuery18(t2.getOutX1());//地址
//                fxyGzgl02Service.save(t02);
            }
        }
        if("2".equals(sqltype)){        //生成借款人
            for(org.jeecg.modules.gzgl.api.apientity.ResultBean t2:  res){
//                FxyGzgl02 t02 = new FxyGzgl02();
//                t02.setQuery07(t2.getOutX1());//订单编号
//                t02.setQuery12(t2.getOutX1());//个人用户的手机号
//                t02.setQuery13(t2.getOutX1());//用户姓名
//                t02.setQuery14(t2.getOutX1());//个人用户证件类型
//                t02.setQuery15(t2.getOutX1());//个人用户证件号码
//                t02.setQuery16(t2.getOutX1());//公证处标识
//                t02.setQuery17(t2.getOutX1());//邮箱
//                t02.setQuery18(t2.getOutX1());//地址
//                fxyGzgl02Service.save(t02);
            }
        }
        if("3".equals(sqltype)){        //担保人
            for(org.jeecg.modules.gzgl.api.apientity.ResultBean t2:  res){
//                FxyGzgl02 t02 = new FxyGzgl02();
//                t02.setQuery07(t2.getOutX1());//订单编号
//                t02.setQuery12(t2.getOutX1());//个人用户的手机号
//                t02.setQuery13(t2.getOutX1());//用户姓名
//                t02.setQuery14(t2.getOutX1());//个人用户证件类型
//                t02.setQuery15(t2.getOutX1());//个人用户证件号码
//                t02.setQuery16(t2.getOutX1());//公证处标识
//                t02.setQuery17(t2.getOutX1());//邮箱
//                t02.setQuery18(t2.getOutX1());//地址
//                fxyGzgl02Service.save(t02);
            }
        }
        if("11".equals(sqltype)){       //生成合同
         //同时生成
            for(org.jeecg.modules.gzgl.api.apientity.ResultBean t2:  res){
                FxyGzgl04 t04 = new FxyGzgl04();
                t04.setQuery07(t2.getOutX1());//订单编号
                t04.setQuery12(t2.getOutX1());//个人用户的手机号
                t04.setQuery13(t2.getOutX1());//用户姓名
                t04.setQuery14(t2.getOutX1());//个人用户证件类型
                t04.setQuery15(t2.getOutX1());//个人用户证件号码
                t04.setQuery16(t2.getOutX1());//公证处标识
                t04.setQuery17(t2.getOutX1());//邮箱
                t04.setQuery18(t2.getOutX1());//地址
                fxyGzgl04Service.save(t04);
            }
        }
        resulFxy.setResultCode("success");
        resulFxy.setResultDesc(result);
        return resulFxy;
    }

    /**
     * 添加
     *
     * @param comEntity
     * @return
     */
    @PostMapping(value = "/asynchronous")
    public ResulFxy<?> asynchronous(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) comEntity comEntity) {
        log.info("comEntity=" + comEntity.toString());
        log.info("comEntity=" + comEntity.toString());
        String eventType = request.getParameter("eventType");
        String userId = request.getParameter("userId");
        String auditState = request.getParameter("auditState");
        String auditDesc = request.getParameter("auditDesc");
        String failDesc = request.getParameter("failDesc");
        ResulFxy resulFxy = new ResulFxy();
//       eventType = 1050
//       企业用户提交的小额鉴权认证，审核及打款结果由公证云服务发起异步回调通知第三方服务
//       名称	必填	类型	默认值	备注
//       userId	是	String(32)		用户Id
//       auditState	是	String		用户实名认证状态，9为认证完成，4为 打款成功
//       auditDesc	是	String		认证状态说明
//       failDesc	否	String		本次实名认证结果（错误）原因，仅失败才有该值
        if (StringUtil.isEmpty(eventType)) {
            eventType = comEntity.getEventType();
            if (StringUtil.isEmpty(eventType)) {
                resulFxy.setResultCode("false");
                resulFxy.setResultDesc("异步操作失败");
                return resulFxy;
            }else{
                  userId = comEntity.getUserId();
                  auditState = comEntity.getAuditState();
                  auditDesc = comEntity.getAuditDesc();
                  failDesc = comEntity.getFailDesc();
            }
        }
        if ("1050".equals(eventType)) {
            List<FxyGzgl03> list03 =   fxyGzgl04Controller.get03by24(userId);
            if(list03!=null&&list03.size()>0){
                FxyGzgl03 t = list03.get(0);
                t.setQuery26(auditState+auditDesc+failDesc);
                fxyGzgl03Service.updateById(t);
            }
        }
        resulFxy.setResultCode("success");
        resulFxy.setResultDesc("异步操作成功");
        return resulFxy;
    }
}
